package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.controller.util.Code;
import com.example.controller.util.Data;
import com.example.domain.*;
import com.example.service.IActivityService;
import com.example.service.ReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/reservation")
public class ReservationController {
    @Autowired
    private ReservationService reservationService;
    @Autowired
    private IActivityService activityService;

    /**
     *        0：拒绝
     * status:1 受理
     *       :2 通过
     * @return
     */
    @GetMapping("/page/{status}/{page}/{size}")
    public Data getPageVolunteer(@PathVariable Integer status,@PathVariable Integer page,@PathVariable Integer size){
        IPage<Reservation> pageVolunteer = reservationService.getPageReservation(page, size,status);
        Integer code = pageVolunteer != null ? Code.Code_ok : Code.Code_err;
        String msg = pageVolunteer != null ? "查询成功" : "查询失败";
        return new Data(code,pageVolunteer, msg);
    }
//    查询该用户参与的所有活动
    @GetMapping("/all/{id}")
    public Data getAll(@PathVariable Integer id){
        LambdaQueryWrapper<Reservation> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Reservation::getVolunteerId,id);
        List<Reservation> list= reservationService.list(queryWrapper);
        Integer code = list==null ? Code.Code_err :Code.Code_ok;
        String msg = list == null ? "query_err" : "query_ok";
        System.out.println(list);
        return new Data(code,list,msg);
    }


    /**
     * 用户进入活动报名页面，进行报名
     * 判断报名时间（是否开启预约！）
     * @param reservation
     * @return
     */
    @PostMapping()
    public Data save(@RequestBody Reservation reservation){
        Data data = reservationService.commitReservation(reservation);
        return data;
    }

    /**
     * 管理员审核通过该报名
     * 报名人数+1
     * @param reservation
     * @return
     */
    @PutMapping()
    public Data updateStatusByAccess(@RequestBody Reservation reservation){
        Data data=reservationService.examine(reservation);
        return data;
    }


    /**
     * 查询当前用户参加的活动信息
     */
    @GetMapping("/{id}/{status}")
    public Data getByStatus(@PathVariable Integer id ,@PathVariable Integer status){
        List<Reservation> list = reservationService.getReservationByStatus(id,status);
        Integer code = list==null ? Code.Code_err :Code.Code_ok;
        String msg = list == null ? "query_err" : "query_ok";
        System.out.println(list);
        return new Data(code,list,msg);
    }


}
